home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / microcrn / issue_41.arc / KAYPRO41.ARC / NOCMNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-15  |  2.0 KB  |  84 lines

  1. /* Figure from Kaypro column in Micro Cornucopia Magazine Issue #41
  2. *  NOCMNT      vers. 6/30/87
  3. *
  4. *  Removes comments, trailing white space, and blank
  5. *  lines from assembly language source files.
  6. *
  7. *  Written for Small-C compiler vers. 2.03 (ASM)
  8. *
  9. *  Note:  does not properly handle literal semicolons  
  10. *  such as in DB ';' or CPI ';' or MVI A,';'.
  11. */
  12.  
  13. #include <stdioa.h>
  14. #include "iolib.asm"
  15. #include "call.asm"
  16.  
  17. #define MAXLINE 127
  18. #define NOCCARGC
  19. #define NOTFOUND -1
  20.  
  21. char line[MAXLINE + 1];
  22.  
  23. main() {
  24.  
  25.   int len, i, j;
  26.  
  27.   fputs("NOCMNT -- strips comments from .ASM files.\n",stderr);
  28.   fputs("Usage: nocmnt <fatfile.asm >slimfile.asm\n",stderr);
  29.  
  30.   while ((len = getline(line, MAXLINE, stdin)) != NULL) {
  31.        i = index(line,';');
  32.        if (i == NOTFOUND) i = len;
  33.        while (--i >= 0)         /* find last non-blank char */ 
  34.           if ((line[i] != ' ') 
  35.           && (line[i] != '\t') 
  36.           && (line[i] != '\n'))
  37.              break;
  38.        if (i == -1) continue;   /* line is blank, so ignore it */
  39.        line[i+1] = '\0';        /* otherwise mark new end      */
  40.        fputs(line,stdout);      /* and send string to output   */
  41.        fputc('\n',stdout);      /* followed by newline char    */
  42.   }
  43. }
  44.  
  45.  
  46. /*
  47. *  Return position (i.e., array index) of first occurrence
  48. *  of c in str, else -1. 
  49. */
  50.  
  51. index(str, c) char *str, c; {
  52.   int i;
  53.   i = 0;
  54.   while(*str) {
  55.     if(*str == c) return (i);
  56.     ++str;
  57.     ++i;
  58.   }
  59.   return (-1);
  60. }
  61.  
  62. /*
  63. *   Fetch a line of input from fd and store it
  64. *   (including any terminating newline) in s.
  65. *   Return the length of the fetched line.  From
  66. *   Kernighan and Ritchie, The C Programming
  67. *   Language, copyright 1978.
  68. */
  69.  
  70. getline(s, lim, fd) char s[]; int lim, fd; {
  71.  
  72.    int c, i;
  73.    
  74.    i=0;
  75.    while (--lim > 0
  76.       && (c=fgetc(fd)) != EOF
  77.       && c != '\n')
  78.          s[i++] = c;
  79.    if (c == '\n')
  80.          s[i++] = c;
  81.    s[i] = '\0';
  82.    return(i);
  83. }
  84.